package com.ricky.base.page;


import org.springframework.data.domain.Sort;

import java.util.List;

import static org.springframework.data.domain.Sort.by;

/**
 * @Author Ricky
 * @Date 2024/6/20 14:08
 * @Description: 排序封装类
 */
public class SortTools {
    public static Sort basicSort() {
        return basicSort("desc", "id");
    }
    public static Sort basicSort(String orderType, String orderField) {
        Sort sort = by(Sort.Direction.fromString(orderType), orderField);
        return sort;
    }
    public static Sort basicSort(SortDto... dtos) {
        SortDto sortDto=new SortDto("desc");
        Sort result = null;
        for(int i=0; i<dtos.length; i++) {
            SortDto dto = dtos[i];
            if(result == null) {
                result = by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField());
            } else {
                result = result.and(by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField()));
            }
        }
        return result;
    }
    public static Sort basicSortSortDto(List<SortDto> dtos) {
        Sort result = null;
        for(int i=0; i<dtos.size(); i++) {
            SortDto dto = dtos.get(i);
            if(result == null) {
                result = by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField());
            } else {
                result = result.and(by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField()));
            }
        }
        return result;
    }
    public static Sort basicSort(String[]... dtos) {
        Sort result = null;
        for(int i=0; i<dtos.length; i++) {
           String  orderType=dtos[i][0];
           String  orderField=dtos[i][1];
           if(orderType.equals("desc")||orderType.equals("DESC")||orderType.equals("asc")||orderType.equals("ASC")){
               SortDto dto = new SortDto(orderType, orderField);
               if(result == null) {
                   result = by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField());
               } else {
                   result = result.and(by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField()));
               }
           }
        }
        return result;
    }
    public static Sort basicSortString(List<String[]> dtos) {
        Sort result = null;
        for(int i=0; i<dtos.size(); i++) {
            String  orderType=dtos.get(i)[0];
            String  orderField=dtos.get(i)[1];
            if(orderType.equals("desc")||orderType.equals("DESC")||orderType.equals("asc")||orderType.equals("ASC")){
                SortDto dto = new SortDto(orderType, orderField);
                if(result == null) {
                    result = by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField());
                } else {
                    result = result.and(by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField()));
                }
            }
        }
        return result;
    }
    public static Sort basicSort(String... dtos) {
        Sort result = null;
        for(int i=0; i<dtos.length; i++) {
            SortDto dto = new SortDto("DESC", dtos[i]);
            if(result == null) {
                result = by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField());
            } else {
                result = result.and(by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField()));
            }
        }
        return result;
    }
    public static Sort basicSort(List<String> dtos) {
        Sort result = null;
        for(int i=0; i<dtos.size(); i++) {
            SortDto dto = new SortDto("DESC", dtos.get(i));
            if(result == null) {
                result = by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField());
            } else {
                result = result.and(by(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField()));
            }
        }
        return result;
    }
}
